package cliente;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

import org.jvnet.substance.SubstanceLookAndFeel;

import util.SocketManager;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.sql.SQLException;

public class ventanaLogin extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JPasswordField passwordField;
	private JTextField textField_1;
	private JTextField textField_2;
	private SocketManager s;
	private String sentence;
	private String sentence2;
	private JButton btnConectar;
	final static String CRLF = "\r\n";

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ventanaLogin frame = new ventanaLogin();
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public ventanaLogin() {
		
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		setTitle("Login");
		JFrame.setDefaultLookAndFeelDecorated(true);
		SubstanceLookAndFeel.setSkin("org.jvnet.substance.skin.BusinessBlueSteelSkin");

		JLabel lblNombreDeUsuario = new JLabel("Nombre de Usuario:");
		lblNombreDeUsuario.setBounds(53, 128, 112, 22);
		contentPane.add(lblNombreDeUsuario);

		JLabel lblContrasea = new JLabel("Contrase\u00F1a:");
		lblContrasea.setBounds(52, 161, 83, 14);
		contentPane.add(lblContrasea);

		textField  = new JTextField();
		textField.setBounds(203, 129, 143, 21);
		contentPane.add(textField);
		textField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(203, 157, 143, 22);
		contentPane.add(passwordField);

		JButton btnAceptar = new JButton("Aceptar");
		btnAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					sentence ="USER";
					sentence2 ="PASS";

					String nick = textField.getText();
					String pass =new String( passwordField.getPassword());
					if(!nick.equals("")){
						
						sentence=sentence.concat(" "+nick);
					}
					if(!pass.equals("")){
						sentence2=sentence2.concat(" "+pass);
					}

					s.Escribir(sentence+CRLF);
					String resp = s.Leer();
	
					if(resp.contains("200 OK"))
					{
						s.Escribir(sentence2+CRLF);
						String resp2 = s.Leer();
						
						if(resp2.contains("201 OK")){
							
							//cargar ventanaControl
							setVisible(false);
							ventanaControl v = new ventanaControl(s);
							v.setLocationRelativeTo(null);
							v.setVisible(true);
							
						}
						else if(resp2.contains("401 ERR")){
							
							JOptionPane.showMessageDialog(null, resp2,"Error", JOptionPane.ERROR_MESSAGE);
						}
						else if(resp2.contains("402 ERR")){
							
							JOptionPane.showMessageDialog(null,resp2,"Error", JOptionPane.ERROR_MESSAGE);
						}	
					}
					else if(resp.contains("400 ERR"))
					{
						
						JOptionPane.showMessageDialog(null, resp,"Error", JOptionPane.ERROR_MESSAGE);
					}
					else if(resp.contains("401 ERR")){
						JOptionPane.showMessageDialog(null, resp,"Error", JOptionPane.ERROR_MESSAGE);
					}
					else if(resp.contains("Permiso denegado")){
						JOptionPane.showMessageDialog(null, resp,"Error", JOptionPane.ERROR_MESSAGE);
					}
								
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		btnAceptar.setBounds(290, 227, 89, 23);
		contentPane.add(btnAceptar);
		
		final JLabel lblIp = new JLabel("IP:");
		lblIp.setBounds(53, 32, 30, 22);
		contentPane.add(lblIp);
		
		final JLabel lblPuerto = new JLabel("Puerto:");
		lblPuerto.setBounds(242, 36, 46, 14);
		contentPane.add(lblPuerto);
		
		textField_1 = new JTextField();
		textField_1.setText("127.0.0.1");
		textField_1.setBounds(79, 33, 86, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		textField_2 = new JTextField();
		textField_2.setText("6968");
		textField_2.setBounds(290, 33, 94, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);
		
		btnConectar = new JButton("Conectar");
		btnConectar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(!textField_1.getText().equals("")){
					try {
						s = new SocketManager(new Socket(textField_1.getText(), Integer.parseInt(textField_2.getText())));
						JOptionPane.showMessageDialog(null, "La conexion se ha realizado!");
						btnConectar.setEnabled(false);
					}  catch (UnknownHostException e) {
						// TODO Auto-generated catch block
						JOptionPane.showMessageDialog(null,"No existe esa IP","Error", JOptionPane.ERROR_MESSAGE);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						JOptionPane.showMessageDialog(null,"No existe esa IP","Error", JOptionPane.ERROR_MESSAGE);
					}

				}
				else{
					JOptionPane.showMessageDialog(null,"Debe introducir una direccion IP","Error", JOptionPane.ERROR_MESSAGE);
				}
			}
			
		});
		btnConectar.setBounds(161, 70, 89, 23);
		contentPane.add(btnConectar);
		
		JButton btnSalir = new JButton("Salir");
		btnSalir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(s!=null){
					try {
						s.CerrarStreams();
						s.CerrarSocket();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
				System.exit( 0 );
			}
		});
		btnSalir.setBounds(29, 227, 89, 23);
		contentPane.add(btnSalir);
		
		
		
	
	}
}
